--[[


	THIS FILE CONTAINS ALL THE CONSOLE COMMANDS THAT CAN BE DONE

]]

gdatabase.commands = {}

--lists all current connections that gdatabase has made
function gdatabase.commands.ListAllCurrentConnections( ply, command, arguments )

	if ( !gdatabase.lib.playercanrun(ply)) then return end
	Msg("INDEX               DATABASE NAME                    STATUS\n")
	Msg("-----------------------------------------------------------\n")
	for i,v in pairs(gdatabase.connections) do
		if(v.databasename ~= nil) then
			Msg(i .. "                   " .. v.databasename .. "(" .. v.databasehost .. ")                    " .. tostring(v.status) .. "\n")
		end
	end
end

--this function kills all the open connections
function gdatabase.commands.KillAllCurrentConnections( ply, command, arguments )
	if ( !gdatabase.lib.playercanrun(ply) ) then return end
	for i,v in pairs(gdatabase.connections) do
		gdatabase.KillConnection(i)
	end
	
	Msg("GDATABASE: All connections disabled\n")

end


function gdatabase.commands.KillConnection(ply, command, arguments)
	if ( !gdatabase.lib.playercanrun(ply) ) then return end
	if arguments[1] == nil then
		Msg("GDATABASE: Please Specify a connection\n")
		return
	else
		
		gdatabase.KillConnection((tonumber(arguments[1])))
		return
	end
return


end

--this function reconnects to all the open connections
function gdatabase.commands.ReconnectAllCurrentConnections( ply, command, arguments )
	if ( !gdatabase.lib.playercanrun(ply) ) then return end
	for i,v in pairs(gdatabase.connections) do
		gdatabase.lib.killconnection(i)
		ptimer.Remove("gdatabase_reconnecttimer_index" .. i)
		gdatabase.lib.createconnection(i)
		gdatabase.lib.keepalive(i)		
	end
	Msg("GDATABASE: All connections reconnected!\n")
end

function gdatabase.commands.ReconnectConnection( ply, command, arguments)
	if ( !gdatabase.lib.playercanrun(ply) ) then return end
	local theIndex = tonumber(arguments[1])
	
	gdatabase.lib.killconnection(theIndex)
	ptimer.Remove("gdatabase_reconnecttimer_index" .. theIndex)
	gdatabase.lib.createconnection(theIndex)
	gdatabase.lib.keepalive(theIndex)	
end
concommand.Add("gdatabase_connections", gdatabase.commands.ListAllCurrentConnections)
concommand.Add("gdatabase_killall", gdatabase.commands.KillAllCurrentConnections)
concommand.Add("gdatabase_kill", gdatabase.commands.KillConnection)
concommand.Add("gdatabase_reconnect", gdatabase.commands.ReconnectConnection)
concommand.Add("gdatabase_reconnectall", gdatabase.commands.ReconnectAllCurrentConnections)


--debug commands
if gdatabase.config.debug then

	function gdatabase.commands.ListThreads( ply, command, arguments )
		for i,v in pairs(gdatabase.connections) do
			print("----------------------------------------------------")
			print("CONNECTION #" .. tostring(i))
			PrintTable(v.threads)
		end
	end
	concommand.Add("gdatabase_listthreads", gdatabase.commands.ListThreads)

end
